package com.atguigu.lease.web.admin.controller.system;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "后台用户信息管理")
@RestController
@RequestMapping("/admin/system/user")
public class SystemUserController {
@Autowired
private SystemUserService systemUserService;

    @Operation(summary = "根据条件分页查询后台用户列表")
    @GetMapping("page")
    public Result<IPage<SystemUserItemVo>> page(@RequestParam long current, @RequestParam long size, SystemUserQueryVo queryVo) {
       //创建Page对象
        Page<SystemUserItemVo> systemUserItemVoPage = new Page<>(current,size);
        //调用分页及带条件查询方法
        IPage<SystemUserItemVo> iPage = systemUserService.getPageList(systemUserItemVoPage,queryVo);
        return Result.ok(iPage);
    }

    @Operation(summary = "根据ID查询后台用户信息")
    @GetMapping("getById")
    public Result<SystemUserItemVo> getById(@RequestParam Long id) {

        //调用根据id查询用户详细的方法
        SystemUserItemVo systemUserItemVo = systemUserService.getSystemUserById(id);
        return Result.ok(systemUserItemVo);
    }

    @Operation(summary = "保存或更新后台用户信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemUser systemUser) {
        //获取用户的密码
        String password = systemUser.getPassword();
        //判断密码是否为空
      /*  if (password != null && password != ""){
            //对明文的密码加密之后再设置进去
            systemUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        }*/
        //判断是更新还是添加
        if (systemUser.getId() == null){
          //给用户设置一个默认的头像
            systemUser.setAvatarUrl("http://192.168.6.100:9000/test/ran.jpg");
            //对密码加密
            systemUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
            //设置用户的状态为可用
            systemUser.setStatus(BaseStatus.ENABLE);
        }else {
            //判断密码是否为空
            if (StringUtils.isEmpty(password)){
                //证明编辑用户的时候没有更新密码，我们如何操作
                //方案一：将原来数据库的密码查询出来再设置进去
                //方案二：将密码设置为null
                systemUser.setPassword(null);
            }else {
                //对密码进行MD5加密
                systemUser.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
            }
        }
        //调用自带的保存或更新的方法
        systemUserService.saveOrUpdate(systemUser);
        return Result.ok();
    }

    @Operation(summary = "判断后台用户名是否可用")
    @GetMapping("isUserNameAvailable")
    public Result<Boolean> isUsernameExists(@RequestParam String username) {
         //调用自带的getOne方法查询一个对象
        SystemUser systemUser = systemUserService.getOne(new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, username));
        return Result.ok(systemUser==null);
    }

    @DeleteMapping("deleteById")
    @Operation(summary = "根据ID删除后台用户信息")
    public Result removeById(@RequestParam Long id) {
      //调用用自带的根据id删除的方法
       systemUserService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID修改后台用户状态")
    @PostMapping("updateStatusByUserId")
    public Result updateStatusByUserId(@RequestParam Long id, @RequestParam BaseStatus status) {
        //调用自带的根据条件更新的方法
        systemUserService.update(new LambdaUpdateWrapper<SystemUser>().eq(SystemUser::getId,id).set(SystemUser::getStatus,status));
        return Result.ok();
    }

    public static void main(String[] args) {
        //使用MD5加密
        String encoded = DigestUtils.md5DigestAsHex("123456".getBytes());
        System.out.println("encoded =" + encoded);
        String encoded2 = DigestUtils.md5DigestAsHex("666666".getBytes());
        System.out.println("encoded2 = " + encoded2);
    }
}
